package com.haois.mp.database.module.offaccounts.rest;

import com.github.pagehelper.PageHelper;
import com.haois.mp.database.commom.enums.ApiResultEnum;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.commom.constant.Reply;

import com.haois.mp.database.module.common.daomain.RemindDob;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.offaccounts.service.RemindDobService;
import com.haois.mp.database.module.common.service.IUserTokenService;
import com.haois.mp.database.util.jx.DateUtil;
import com.haois.mp.database.util.jx.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/9/18 22:15
 */
@RestController
@RequestMapping("/wxmp/rest/remind")
public class RemindRestController {
    @Autowired
    private RemindDobService remindDobService;
    @Autowired
    private IUserTokenService iUserTokenService;

    @PostMapping(value = "/birthday/updateInfo", params = {"name", "birthdayStr", "birthdayType"})
    public String addBirthday(HttpServletRequest request, RemindDob remindDob, String birthdayStr) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());

        Date date = DateUtil.stringToDate(birthdayStr, DateUtil.yMd);
        if (date == null) {
            return new Reply(ApiResultEnum.API_10.getCode(), "出生时间格式错误").toJson();
        } else {
            remindDob.setBirthday(date);
        }
        remindDob.setMemberNum(userToken.getUserId());
        remindDobService.insert(remindDob);
        return new Reply<>(ApiResultEnum.API_00).toJson();
    }

    @GetMapping("/birthday/list")
    public String birthdayList(HttpServletRequest request, String beginTime, String endTime) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        PageHelper.startPage(request);
        List<RemindDob> remindDobs = remindDobService.findByMember(userToken.getUserId(), beginTime, endTime);
        remindDobs.forEach(RemindDob::setOnAgeD);
        return new Reply(ApiResultEnum.API_00, remindDobs).toJson();

    }

    @GetMapping("/nearEvent/list")
    public String nearRemind(HttpServletRequest request) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());

        TreeSet treeSet = getTreeSet2();
        Date date = new Date();
        PageHelper.startPage(request);
        List<RemindDob> remindDobList = remindDobService.findNearByMember(userToken.getUserId());
        remindDobList.forEach(remindDob -> {
            int days = DateUtil.daysBetween(date, remindDob.getOnBirthday());
            int days1 = DateUtil.daysBetween(date, remindDob.getNextBirthday());
            remindDob.setAge((days < 31 && days > -8) ? days : (days1 < 31 && days1 > -8) ? days1 : 999);
            remindDob.setOnAgeD();
            treeSet.add(remindDob);
        });
        return new Reply(ApiResultEnum.API_00, remindDobList).toJson();
    }

    @GetMapping("/otherEvent/list")
    public String otherRemind(HttpServletRequest request) {
        String access_token = (String) request.getAttribute("access_token");
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        return new Reply(ApiResultEnum.API_00, new ArrayList<>()).toJson();
    }

    @RequestMapping(value = "/birthday/getInfo", method = RequestMethod.GET,
            params = {"id"})
    public String getBirthday(int id) {
        RemindDob driver = remindDobService.findById(id);
        return GsonUtil.toJson(driver);
    }

    public TreeSet getTreeSet() {
        TreeSet treeSet = new TreeSet((o1, o2) -> {
            try {
                RemindDob r1 = (RemindDob) o1;
                RemindDob r2 = (RemindDob) o2;
                return r1.getOnBirthday().compareTo(r2.getOnBirthday());
            } catch (Exception e) {
                return 0;
            }
        });
        return treeSet;
    }

    public TreeSet getTreeSet2() {
        TreeSet treeSet = new TreeSet((o1, o2) -> {
            try {
                RemindDob r1 = (RemindDob) o1;
                RemindDob r2 = (RemindDob) o2;
                return r1.getAge().compareTo(r2.getAge());
            } catch (Exception e) {
                return 0;
            }
        });
        return treeSet;
    }
}
